**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/LEMAS_complaints.log, text replace
set more off

use ${rep_root}/data/LEMAS/38651-0001-Data, clear
 
do ${rep_root}/data/LEMAS/38651-0001-Supplemental_syntax.do
gen year = 2020
 

rename ORI9 ORI9 
drop if ORI9 == "" 
keep if AGENCYSAMPTYPE == 3

rename AGENCYNAME agency_name

gen popgroup = 1 if PRIMARYPOP2020 <2500
replace popgroup = 2 if PRIMARYPOP2020 >=2500 & PRIMARYPOP2020<7500
replace popgroup = 3 if PRIMARYPOP2020 >=7500 & PRIMARYPOP2020<25000
replace popgroup = 4 if PRIMARYPOP2020 >=25000 & PRIMARYPOP2020<100000
replace popgroup = 5 if PRIMARYPOP2020 >=100000 & PRIMARYPOP2020<250000
replace popgroup = 6 if PRIMARYPOP2020 >=250000 & PRIMARYPOP2020<.
 
gen sustain_rate = COMPL_ALL_SUST /(COMPL_ALL_SUST+COMPL_ALL_OTH)
gen sustain_force_rate = COMPL_UOF_SUST /(COMPL_UOF_SUST+ COMPL_UOF_OTH)


gen civ_board = CIV_COMPL == 1  
replace civ_board = . if CIV_COMPL ==.
gen pol_complaints = POL_COMPL == 1  
replace pol_complaints = . if POL_COMPL ==.

 
gen comp_rate = COMPL_ALL_TOT/FTSWORN
gen comp_force_rate = COMPL_UOF_TOT/FTSWORN
 
drop if COMPL_ALL_TOT ==. | COMPL_ALL_SUST ==.
  
eststo clear 
eststo all: estpost summarize civ_board pol_complaints   comp_rate   sustain_rate  

forval i = 1/6{
eststo g`i': estpost summarize civ_board pol_complaints   comp_rate   sustain_rate  if popgroup ==`i'
}
  
 
 

esttab  all g1 g2 g3 g4 g5 g6 using "${rep_root}/output/tables/LEMAS_comp_rates.tex" ,  replace tex coeflabel(comp_rate "Complaint/Sworn Officers"  civ_board "Civilian Board"  pol_complaints "Written Policy"  sustain_rate "Sustain Rate"  ) main(mean %9.3gc) aux(sd %9.3gc) collabels(none) cells("mean(pattern(1 1 1 1 1 1  ) fmt(2)) b(star pattern(0 0 0 0 0 0 0 ) fmt(%9.3f))" "sd(pattern(1 1 1 1 1 1 1) par fmt(%9.3f))  se(pattern(0 0 0 0 0 0 0 ) par fmt(%9.3f))") ///
modelwidth(20) mgroups("Mean (SD) in Population Groups"  , ///
	pattern(1 0 0 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  mtitles("All" "$<$ 2500" "2500-7500" "7500-25K" "25K-100K"  "100K-250K" "$>$250K" )   starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, labels("Observations") fmt(%9.3gc))  
 
  

  
  
*******************
*See complaints numbers in Chicago POlice DEpartment 
*******************
**Read main complants data
use ${rep_root}/data/complaints_2021, clear

gen year = year(inc_complaint_dt_1)
tab year 
tab finding_cd if year == 2018

**Read recent complaints data
use ${rep_root}/data/complaints_cms, clear

**Standardize finding variable across datasets
rename recommended_finding finding_cd
replace finding_cd = upper(finding_cd) 

gen year = year(inc_complaint_dt_1)
tab year 

 